package com.allen.common

import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.ViewDataBinding
import androidx.fragment.app.Fragment
import com.alibaba.android.arouter.launcher.ARouter
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.BasePopupView
import com.trello.rxlifecycle2.components.support.RxAppCompatActivity

/**
 *  @Autor  lwl
 *  日期    2021/4/7
 *  目的
 */
open abstract class BaseFragment<T : ViewDataBinding, V : BaseVM> : Fragment() {
    protected var mActivity: RxAppCompatActivity? = null

    var mContext: Context? = null

    var mBaseViewModel: V? = null

    var mBinding: T? = null

    protected var mLoadingDialog: BasePopupView? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mActivity = activity as RxAppCompatActivity?
        ARouter.getInstance().inject(this)
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(initLayout(), null)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        if (mLoadingDialog == null) {
            mLoadingDialog = XPopup.Builder(mActivity!!.baseContext)
                .asCustom(CustomLoadingPopup(mActivity!!.baseContext))
        }
        if (mLoadingDialog!!.isShow) {
            mLoadingDialog?.dismiss()
        } else {
            mLoadingDialog?.show()
        }

        initDate()
        initViewDate()
        initEvent()
        initObserve()
    }

    /**
     * 初始化布局
     *
     * @return 布局id
     */
    protected abstract fun initLayout(): Int

    /**
     * 初始化标题栏名称
     *
     * @return 标题栏名称
     */
    protected abstract fun initTitleName(): String? //设置标题栏


    /**
     * 初始化基本数据
     */
    protected abstract fun initDate()

    /**
     * 初始化控件数据
     */
    protected abstract fun initViewDate()

    /**
     * 初始化控件事件
     */
    protected abstract fun initEvent()

    /**
     * 初始化观察者
     */
    protected abstract fun initObserve()
}
